package com.example.my_objectives.controller;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;

public class MainController extends DatabaseController {

	public MainController(Context context) {
		super(context);
	}

	public ArrayList<ContentValues> getAllObjectiveStatus() {
		ArrayList<ContentValues> list = new ArrayList<ContentValues>();
		try {
			String query = "SELECT objective.id, objective.name, description, sum(current_value) AS total_values "
				+ "FROM objective, history WHERE objective.id = id_objective "
				+ "GROUP BY objective.id, objective.name, description " + "ORDER BY objective.name";
			Log.d(MainController.class.toString(), query);

			Cursor cursor = get_database().rawQuery(query, null);

			if (cursor.moveToFirst()) {
				while (!cursor.isAfterLast()) {
					ContentValues record = new ContentValues();
					record.put("id", cursor.getString(cursor.getColumnIndex("id")));
					record.put("name", cursor.getString(cursor.getColumnIndex("name")));
					record.put("description", cursor.getString(cursor.getColumnIndex("description")));
					record.put("total_values", cursor.getString(cursor.getColumnIndex("total_values")));

					list.add(record);
				}// while
			}// if

		}// try
		catch (Exception e) {
			Log.e(MainController.class.toString(), e.getMessage());
			list = null;
		}// catch
		return list;
	}
}
